//
//  MakeDetialTableViewController.m
//  BakeCenter
//
//  Created by lanou3g on 15/9/21.
//  Copyright (c) 2015年 Baked. All rights reserved.
//

#import "MakeDetialTableViewController.h"
#import "MakeDetialTableViewCell.h"
#import "MakeDetialModel.h"
#import "stepModel.h"
#import "materialModel.h"
#import "MaterialTableViewCell.h"
#import "TableHeadVew.h"
#import "HeadImageView.h"
@interface MakeDetialTableViewController ()

@property (nonatomic)CGFloat lableHeight;
@property (nonatomic, strong)HeadImageView *headView;
@property (nonatomic, strong)NSMutableArray *stepArray;
@property (nonatomic, strong)NSMutableArray *materialArray;


@end

@implementation MakeDetialTableViewController


//-(NSMutableArray*)retrunCookies{
//    NSDictionary *properties = [[NSMutableDictionary alloc] init];
//    [properties setValue:@"11ba883c7949ecd7ce1716ceee621bb8|1442997433|1442994375" forKey:@"SERVERID"];
//    [properties setValue:@"7003280is5rrqdic5pg9gnhad2" forKey:@"USERID"];
//    [properties setValue:@"ios#iPhone 6 (A1549/A1586)#8.1.2#3.4.0#750#1334#appStore#Wifi#87.64#com.xiangha.XiangHa1#1#61036501740" forKey:@"device"];
//    [properties setValue:@"xhapp#ios#3.4.0" forKey:@"User-Agent"];
//    [properties setValue:@"" forKey:@"userCode"];
//    [properties setValue:@"c82cbde50ca6f800" forKey:@"webCode"];
//     [properties setValue:@"c4fb64af4db8e43eea533c62eeb7dc7e" forKey:@"xhCode"];
////     [properties setValue:@"" forKey:@""];
//    
//    NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties] ;
//    return [NSMutableArray arrayWithObject:cookie];
//
//}
//制作详情数据解析
-(void)reloadMakeDetialData{
//    if ([self.lastView isEqual:@"classification"]) {
////        [self retrunCookies];
//        [LORequestManger GET:self.makeDetialUrl success:^(id response) {
//            NSDictionary *dic = (NSDictionary *)response;
//            MakeDetialModel *detialModle = [[MakeDetialModel alloc] init];
//            detialModle.coverTitle = dic[@"data"][@"name"];
//            detialModle.coverImage = dic[@"img"];
//            for (NSDictionary *material in dic[@"data"][@"burden"]) {
//                 materialModel *mModel = [[materialModel alloc] init];
//                mModel.name = material[@"name"];
//                mModel.weight = material[@"content"];
//                [self.materialArray addObject:mModel];
//            }
//            for (NSDictionary *step in dic[@"data"][@"makes"]) {
//                stepModel *smodel = [[stepModel alloc] init];
//                smodel.text = step[@"info"];
//                smodel.image = step[@"img"];
//                [self.stepArray addObject:smodel];
//
//            }
//            [self.tableView reloadData];
//        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//            NSLog(@"%@",error);
//        }];
//    }
//   

//    if ([self.lastView isEqualToString:@"recommend"]) {
        [LORequestManger GET:self.makeDetialUrl success:^(id response) {
            NSDictionary *dic = (NSDictionary *)response;
            MakeDetialModel *detialModle = [[MakeDetialModel alloc] init];
            [detialModle setValuesForKeysWithDictionary:dic[@"data"][@"recipe"]];
            self.navigationItem.title = detialModle.coverTitle;
            self.headView.title.text = detialModle.coverTitle;
            [self.headView sd_setImageWithURL:[NSURL URLWithString:detialModle.coverImage] placeholderImage:[UIImage imageNamed:@"10876719,2880,1800.jpg"]];
            for (NSDictionary *step in detialModle.step) {
                stepModel *smodel = [[stepModel alloc] init];
                [smodel setValuesForKeysWithDictionary:step];
                [self.stepArray addObject:smodel];
            }
            for (NSDictionary *material in detialModle.material) {
                materialModel *mModel = [[materialModel alloc] init];
                [mModel setValuesForKeysWithDictionary:material];
                [self.materialArray addObject:mModel];
            }
            [self.tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
//    }
}

-(NSMutableArray *)stepArray{
    if (!_stepArray) {
        _stepArray = [[NSMutableArray alloc] init];
    }
    return _stepArray;
}

-(NSMutableArray *)materialArray{
    if (!_materialArray) {
        _materialArray = [[NSMutableArray alloc] init];
    }
    return _materialArray;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:kScreenBounds style:(UITableViewStyleGrouped)];
     self.tableView.separatorStyle = 0;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    //headView的图片
    self.headView = [[HeadImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 220)];
    self.headView.backgroundColor = [UIColor redColor];
    
    self.tableView.tableHeaderView = self.headView;
    [self.headView.collectButton addTarget:self action:@selector(collectButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.tableView registerClass:[MakeDetialTableViewCell class] forCellReuseIdentifier:@"makeDetial"];
    [self.tableView registerClass:[MaterialTableViewCell class] forCellReuseIdentifier:@"material"];
    
    [self reloadMakeDetialData];
}

//收藏按钮点击事件
-(void)collectButtonClick:(UIButton *)collectButton{
    
    collectButton.selected = !collectButton.selected;
    if (collectButton.selected) {
        
           [self setUpAnimation];
    }else{
        [collectButton setBackgroundImage:[UIImage imageNamed:@"Entypo_2665(0)_128"] forState:(UIControlStateNormal)];
    }
}

-(void)setUpAnimation{
    UIImageView *im = [[UIImageView alloc] initWithFrame:self.headView.collectButton.frame];
    im.tag = 999;
    im.image = [UIImage imageNamed:@"Entypo_2665(22)_128"];
    [self.headView addSubview:im];
    CATransition *animation=[CATransition animation];
    
    animation.delegate=self;
    
    animation.duration=0.8;
    
    animation.timingFunction=UIViewAnimationCurveEaseInOut;
    
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    animation.type = kCATransitionMoveIn;
    animation.subtype = kCATransitionFromBottom;
    [im.layer addAnimation:animation forKey:nil];
    NSLog(@"ddd");
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    UIImageView *imageView = (UIImageView *)[self.headView viewWithTag:999];
    [imageView removeFromSuperview];
    
    [self.headView.collectButton setBackgroundImage:[UIImage imageNamed:@"Entypo_2665(22)_128"] forState:(UIControlStateNormal)];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (section == 0) {
        return self.materialArray.count;
    }else{
        return self.stepArray.count;
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section != 0) {
    MakeDetialTableViewCell *detialCell = [tableView dequeueReusableCellWithIdentifier:@"makeDetial" forIndexPath:indexPath];
        stepModel *model = self.stepArray[indexPath.row];
        detialCell.step.text = model.text;
        [detialCell.stepImage sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"10876719,2880,1800.jpg"]];
        detialCell.number.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
        CGRect contentRect = [detialCell.step.text boundingRectWithSize:CGSizeMake(kScreenWidth / 2 + 100, 1000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
        
        detialCell.step.frame = CGRectMake(detialCell.stepImage.left - 50, detialCell.stepImage.bottom + 5, kScreenWidth / 2 + 100, contentRect.size.height);
        self.lableHeight = detialCell.step.bottom + 25;
        return detialCell;
    }else{
        MaterialTableViewCell *materialCell = [tableView dequeueReusableCellWithIdentifier:@"material" forIndexPath:indexPath];
        materialModel *model = self.materialArray[indexPath.row];
        materialCell.nameLabel.text = model.name;
        materialCell.weightLabel.text = model.weight;
        return materialCell;
        
    }
    
    // Configure the cell...
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section != 0) {
        return self.lableHeight;
    }else{
         return 30;
    }
   
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TableHeadVew *headView = [[TableHeadVew alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    if (section == 0) {
        headView.sectionTitle.text = @"准备材料";
    }else{
        headView.sectionTitle.text = @"开始做吧";
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
